<?php
namespace Admin\Controller;
use Think\Controller;
use \Think\Page;
class CaiwuController extends CommonController {
	public function chongzhi($num='',$username=''){
		if(empty($num)){
			$num = 10;
		}
		$count = M('user_account') -> join('zx_users on zx_users.user_id = zx_user_account.user_id') -> where(['user_name'=>['like','%'.$username.'%']]) -> count();
		// echo $count;die;
		$Page       = new Page($count,$num);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$data = M('user_account') -> field('user_name,true_name,add_time,amount,user_note,admin_note,admin_user') -> join('zx_users on zx_users.user_id = zx_user_account.user_id') -> 
		where(['user_name'=>['like','%'.$username.'%']]) -> order('add_time desc') -> limit($Page->firstRow.','.$Page->listRows) -> select();
		$zongji = M('user_account') -> field('sum(amount) as zongji') -> find();
		$zong = $zongji['zongji'];
		$this -> assign('zong',$zong);
		$this->assign('data',$data);// 赋值数据集
		$this->assign('url',$show);// 赋值分页输出
		$this -> assign('num',$num);
		$this -> display('/caiwu/chongzhi');
	}

	public function add(){
		$this -> display('/caiwu/add');
	}

	public function insert(){
		if(is_numeric(I('post.amount'))){
			M('users') -> startTrans();
			//获取充值表单的数据，并写入添加数组
			$data1 = ['amount'=>I('post.amount'),'admin_note'=>I('post.admin_note'),'user_note'=>I('post.user_note')];
			$data1['add_time'] = time(); 
			$data1['admin_user'] = $_SESSION['admin']['user_name'];
			//获取该用户注册积分
			$account = M('users') -> field('user_id,user_money2') -> where(['user_name'=>I('user_name')]) -> find();
			$data1['user_id'] = $account['user_id'];
			//加上充值的注册积分
			$zhucejifen = $account['user_money2']+I('post.amount');
			//加上的注册积分写入修改数组
			$data2 = ['user_money2'=>$zhucejifen];
			//添加到充值列表中
			$chongzhi = M('user_account') -> data($data1) -> add();
			
			//修改该用户注册积分
			$user = M('users') -> data($data2) -> where(['user_name'=>I('post.user_name')]) -> save();
		
			//封装后台日志
			$admin_log = M('admin_log');
			$ad_log['user_name'] = $_SESSION['admin']['user_name'];
			$ad_log['log_time'] = time();
			$ad_log['user_id'] = $_SESSION['admin']['user_id'];
			$ad_log['log_info'] = '给会员'.$data1['admin_user'].'充值操作';
			$ad_log['ip_address']= get_client_ip();
			$res2 = $admin_log -> add($ad_log);
			//封装前台日志
			$users_log = M('account_log');
			$user_log['user_id'] = $account['user_id'];
			$user_log['user_money2'] = I('post.amount');
			$user_log['change_type'] = 0;
			$user_log['change_time'] = time();
			$user_log['change_desc'] = '充值';
			// dump($chongzhi);
			// dump($user);
			// echo $res2;
			$res4 = $users_log -> add($user_log);
			// dump($res4);die;
			if($chongzhi && $user && $res2 && $res4){
				$this -> success('充值成功');
				$m = M('users') -> commit();
			}else{
				M('users') -> rollback();
				$this -> error('充值失败');
			}
		}else{
			$this -> error('金额必须是数字');
		}
	}

	public function shangjia(){
		$this -> display();
	}

	public function guquan(){
		echo '111';
	}

	//资金管理
	public function zijin(){
		$jiang = M('users') -> field('sum(dongtaikoushui_money) as dtks,sum(cishan_money) as cishan,sum(pay_points1) as points1,sum(pay_points2) as points2,sum(pay_points3) as points3,sum(pay_points4) as points4,sum(pay_points5) as points5,sum(option_num) as option_num,sum(user_money1) as money1,sum(user_money2) as money2,sum(user_money3) as money3,sum(user_money4) as money4,sum(user_money4) as money5') -> find();
		$pa4 = M('param') -> field('param4') -> where(['id'=>1]) -> find();
		$s_all = M('market') -> field('sum(sell_num) as sell_num') -> where(['change_type'=>'option_num','status'=>3,'sell_user_id'=>['neq',0]]) -> find();
		$koushui = $s_all['sell_num']*$pa4['param4']/100;
		$totle = $jiang['points1'] + $jiang['points2'] + $jiang['points3'] + $jiang['points4'] + $jiang['points5'];
		$gongsi = M('users') -> field('tree_num') -> where(['user_id'=>'3000001']) -> find();
		$prc = ($totle/$gongsi['tree_num'])*100;
		$prc = sprintf('%.2f', $prc);
		$this -> assign('koushui',$koushui);
		$this -> assign('prc',$prc);
		$this -> assign('jiang',$jiang);
		$this -> display();
	}
}